<?php

//require_once dirname(__FILE__) . '/../protected/extensions/PHPMailer/class.phpmailer.php';

function pr($arr, $die = true) {
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    if ($die) {
        die();
    }
}

function dump($arr, $die = true) {
    echo "<pre>";
    var_dump($arr);
    echo "</pre>";
    if ($die) {
        die();
    }
}

function read($var) {
    if (isset($var)) {
        $returnVar = $var;
    } else {
        $returnVar = NULL;
    }
    return $var;
}

function printMsg($msg) {
    echo $msg . "\n";
}

function guid() {

    if (function_exists('com_create_guid')) {
        $guid = com_create_guid();
        $guid = preg_replace("/[^A-Za-z0-9]/", "", $guid);
        return $guid;
    } else {
        mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $guid = substr($charid, 0, 8)
                . substr($charid, 8, 4)
                . substr($charid, 12, 4)
                . substr($charid, 16, 4)
                . substr($charid, 20, 12);
        return $guid;
    }
}

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

function changePassword($obj, $post) {
    if (isset($post)) {
        $obj->setAttribute('password', md5($post));
        $obj->save(false); // save the change to database
    }
}

function forgatePasswordEmail($userName, $user, $activationLink, $subject, $msgTpl) {
    try {

        $mail = new PHPMailer(true);

        $mail->IsSMTP();
        $mail->Host = EMAIL_HOST;
        $mail->SMTPAuth = EMAIL_SMTP_AUTH;
        $mail->Username = EMAIL_USER_NAME;
        $mail->Password = EMAIL_PASSWORD;
        $mail->SetFrom(EMAIL_FROM, 'RS');
        $mail->Subject = $subject;
        $msgTpl = Yii::getPathOfAlias('webroot') . '/protected/views/email/' . $msgTpl;
        // $body = $activationKey;
        $body = '';
        if (file_exists($msgTpl)) {
            $body = file_get_contents($msgTpl);
            // $body = str_replace("ACTIVATIONLINK", $activationLink, $body);
            // $body = str_replace('$RESTAURANT_NAME', $user->username, $body);
        }

        $mail->MsgHTML($body);
        $mail->IsHTML(true); // send as HTML
        $mail->AddAddress($userName);
        $mail->Send();
    } catch (Exception $e) {
        
    }
}

function getOffSetForPaging($pageCount, $pageSize) {
    if (!empty($pageCount) && !empty($pageSize)) {
        $start = ($pageCount - 1) * $pageSize;
        $end = ($pageCount * $pageSize) - 1;
        return array('start' => $start, 'end' => $end);
    }
}

function getGender($gender) {
    if ($gender == 1)
        return 'Male';
    else return 'Female';
}

?>
